﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using VOL;
using BLL;

namespace Web.Admin
{
    public partial class NguoiDungForm : System.Web.UI.Page
    {
        NguoiDungBL _nguoiDungBLL = new NguoiDungBL();
        int MaNguoiDung = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string _MaNguoiDung = Request.QueryString["MaNguoiDung"];

                if (_MaNguoiDung == null || _MaNguoiDung == "" || !int.TryParse(_MaNguoiDung, out MaNguoiDung))
                {
                    hdfMaNguoiDung.Value = "0";
                }
                else
                {
                    DataTable dt = _nguoiDungBLL.GetDataById(MaNguoiDung);

                    txtHoTen.Text = dt.Rows[0]["HoTen"].ToString();
                    txtTenTruyCap.Text = dt.Rows[0]["TenTruyCap"].ToString();
                    txtPass.Text = dt.Rows[0]["MatKhau"].ToString();
                    hdfMaNguoiDung.Value = _MaNguoiDung;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string _MaNguoiDung = hdfMaNguoiDung.Value;
            if (_MaNguoiDung == null || _MaNguoiDung == "0")
            {
                // thực hiện thêm mới
                NguoiDung obj = new NguoiDung();
                obj.HoTen = txtHoTen.Text;
                obj.MatKhau = txtPass.Text;
                obj.TenTruyCap = txtTenTruyCap.Text;
                _nguoiDungBLL.InsertData(obj);
            }
            else
            {
                // thực hiện sửa
                NguoiDung obj = new NguoiDung();
                obj.MaNguoiDung = int.Parse(_MaNguoiDung);
                obj.HoTen = txtHoTen.Text;
                obj.MatKhau = txtPass.Text;
                obj.TenTruyCap = txtTenTruyCap.Text;
                _nguoiDungBLL.UpdateData(obj);
            }

            Response.Redirect("~/Admin/NguoiDungManager.aspx");
        }
    }
}